#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
  pid_t id = fork();
  if(id == 0)
  {
    printf("I am child process, pid: %d\n", getpid());
    sleep(5);
    exit(100);
  }
  int status = 0;
  while(1)
  {
    sleep(1);
    pid_t rid = waitpid(id, &status, WNOHANG);
    if(rid > 0)
    {
      printf("wait success!\n");
      if(WIFEXITED(status))
      {
        printf("child exit success! child exit code: %d\n", WEXITSTATUS(status));
        printf("child exit signal: %d\n", (status & 0x7F));
        printf("coredump: %d\n", (status >> 7) % 2);
      }
      else 
      {
        printf("child exit abnormal!\n");
        printf("child exit signal: %d\n", (status & 0x7F));
      }
      break;
    }
    else 
    {
      printf("wait fail!\n");
    }
  }
  return 0;
}
